package com.hand.wyc24548.api.controller.v1;

import com.hand.wyc24548.app.service.SoLineService;
import com.hand.wyc24548.config.SwaggerApiConfig;
import com.hand.wyc24548.domain.entity.SoLine;
import com.hand.wyc24548.domain.repository.SoLineRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wangyc
 * @create 2019/8/9 15:11
 */

@Api(tags = SwaggerApiConfig.SOLINE)
@RestController("soline.v1")
@RequestMapping("/v1/soline")
public class SoLineController extends BaseController {

    private final SoLineRepository soLineRepository;
    private final SoLineService soLineService;

    public SoLineController(SoLineRepository soLineRepository, SoLineService soLineService) {
        this.soLineRepository = soLineRepository;
        this.soLineService = soLineService;
    }

    @Permission(level = ResourceLevel.ORGANIZATION)
    @ApiOperation(value = "分页查询订单行信息")
    @GetMapping
    public ResponseEntity<Page<SoLine>> pageTask(PageRequest pageRequest,SoLine soLine){
        return Results.success(soLineRepository.pageAndSort(pageRequest,soLine));
    }

    /**
     * 新增订单行
     * @param soLine
     * @return
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "新增订单行")
    @PostMapping
    public ResponseEntity<SoLine> create(@RequestBody SoLine soLine){
        this.validObject(soLine);
        return Results.success(soLineService.create(soLine));
    }

    /**
     * 更新订单行
     * @param soLine
     * @return
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "更新订单行")
    @PutMapping
    public ResponseEntity<SoLine> update(@RequestBody SoLine soLine){
        this.validObject(soLine);
        return Results.success(soLineService.update(soLine));
    }

    /**
     * 删除订单行
     * @param soLineId
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "删除订单行")
    @DeleteMapping("/{soLineId}")
    public void delete(@PathVariable @ApiParam(value = "订单行ID") Long soLineId){
/*        SoLine soLine=new SoLine();
        // 数据防篡改校验
        SecurityTokenHelper.validToken(soLine);*/
        soLineService.delete(soLineId);
    }
}
